#include "OpRotate.h"
#include "Qt3DPicker.h"
#include "INode.h"
#include "Registry.h"
#include "ActorTable.h"
#include "Ent.h"

class OpRotatePrivate
{
public:
	std::set<std::shared_ptr<INode>> m_children;
	double m_angle;
	osg::Vec2d m_basePoint;
};

OpRotate::OpRotate(const std::set<std::shared_ptr<INode>>& children,const double angle, const osg::Vec2d& basePoint, QUndoCommand *parent)
	: Op(parent)
	,d_ptr(new OpRotatePrivate)
{
	d_ptr->m_children = children;
	d_ptr->m_basePoint = basePoint;
	d_ptr->m_angle = angle;
}

OpRotate::~OpRotate()
{
	delete d_ptr;
}

#include "QtOSGWidget.h"
void OpRotate::redo()
{
	Registry &reg = Registry::instance();
	auto widget3D = reg.get<QtOSGWidget>(widget3DID);
	widget3D->pauseRenderThread();

	for (auto p : d_ptr->m_children)
	{
		std::shared_ptr<Ent2D> ent = std::dynamic_pointer_cast<Ent2D>(p);
		ent->rotate(d_ptr->m_angle, d_ptr->m_basePoint);
	}
	d_ptr->m_angle = -d_ptr->m_angle;
	widget3D->resumeRenderThread();
}

void OpRotate::undo()
{
	redo();
}
